package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.mapper.MenuMapper;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.*;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 遍历用户/分页查询用户信息
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);//分页操作
        Page<Role> page = roleMapper.findPage(queryString);//查询数据库
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 查询全部角色
     *
     * @return
     */
    @Override
    public List<Role> findAll() {
        List<Role> list = roleMapper.findPage("");
        System.out.println(list);
        return list;
    }

    /**
     * 根据id查询指定角色信息
     *
     * @return
     */
    @Override
    public Role findById(Integer id) {
        return roleMapper.findById(id);
    }

    /**
     * 新增角色(角色包含角色自身以及关联的权限和菜单信息)
     *
     * @param role
     */
    @Override
    public void add(Role role, Integer[] perIds, Integer[] menuIds) {
        //增加角色
        roleMapper.add(role);
        //获取角色id
        Integer role_id = role.getId();
        //建立角色与权限的关联关系;
        Map<String, Integer> perMap = new HashMap<>();
        if (perIds != null && perIds.length > 0) {
            for (Integer perId : perIds) {
                perMap.put("permission_id", perId);
                perMap.put("role_id", role_id);
                roleMapper.setRoleAndPer(perMap);
            }
        }
        //建立角色与菜单的关联关系;
        Map<String, Integer> menuMap = new HashMap<>();
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                menuMap.put("menu_id", menuId);
                menuMap.put("role_id", role_id);
                roleMapper.setRoleAndMenu(menuMap);
            }
        }
    }

    /**
     * 编辑角色
     *
     * @param role
     * @param perIds
     * @param menuIds
     */
    @Override
    public void update(Role role, Integer[] perIds, Integer[] menuIds) {
        //更新角色信息
        roleMapper.update(role);
        Integer id = role.getId();
        if (id != null) {
            //取消角色-权限关联
            roleMapper.deletePerAssociationById(id);
            //取消角色-菜单关联
            roleMapper.deleteMenuAssociationById(id);
        }
        //建立角色-权限关联
        if (perIds != null && perIds.length > 0) {
            Map<String, Integer> perMap = new HashMap<>();
            for (Integer perId : perIds) {
                perMap.put("role_id", id);
                perMap.put("permission_id", perId);
                roleMapper.setRoleAndPer(perMap);
            }
        }
        //建立角色-菜单关联
        if (menuIds != null && menuIds.length > 0) {
            Map<String, Integer> menuMap = new HashMap<>();
            for (Integer menuId : menuIds) {
                menuMap.put("role_id", id);
                menuMap.put("menu_id", menuId);
                roleMapper.setRoleAndMenu(menuMap);
            }
        }
    }

    /**
     * 删除指定角色,如存在关联表则不可删除,并进行提示
     *
     * @param id
     */
    @Override
    public Result delete(Integer id) {
        //查询角色关联的用户
        Integer userNum = roleMapper.findUserCountById(id);
        //查询角色关联的权限
        Integer perNum = roleMapper.findPerCountById(id);
        //查询角色关联的菜单
        Integer menuNum = roleMapper.findMenuCountById(id);

        StringBuffer sb = new StringBuffer();
        if (userNum > 0) {
            sb.append("该角色关联了" + userNum + "位用户,");
        }
        if (perNum > 0) {
            sb.append("该角色关联了" + perNum + "项权限,");
        }
        if (menuNum > 0) {
            sb.append("该角色关联了" + menuNum + "个菜单,");
        }
        if (userNum > 0 | perNum > 0 | menuNum > 0) {
            //存在关联表,不能删除,并返回关联信息
            sb.append("无法删除该角色");
            return new Result(false, sb.toString());
        }

        //不存在关联表,直接删除角色
        roleMapper.delete(id);
        return new Result(true, "删除角色成功");
    }


}
